package com.fan.configService.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fan.configService.mapper.AppResourceRelevanceMapper;
import com.fan.configService.mapper.ResourceMapper;
import com.fan.configService.mapper.ResourceUserMapper;
import com.fan.configService.po.AppResourceRelevance;
import com.fan.configService.po.Application;
import com.fan.configService.po.Resource;
import com.fan.configService.util.ConvertUtil;
import com.fan.configService.vo.MyPage;
import com.fan.configService.vo.ResourceVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * java类简单作用描述
 *
 * @Description: java类作用描述
 * @Author: hf
 * @CreateDate: 2018/9/4 14:53
 * @UpdateUser: hf
 * @UpdateDate: 2018/9/4 14:53
 * @UpdateRemark: The modified content
 * @Version: 1.0
 */
@Service
@Slf4j
public class ResourceService {
    @Autowired
    ResourceMapper resourceMapper;
    @Autowired
    ResourceUserMapper resourceUserMapper;
    @Autowired
    AppResourceRelevanceMapper appResourceRelevanceMapper;
    @Autowired
    ResourceEnvService resourceEnvService;


    public MyPage<ResourceVo> getAllApplicationByPage(MyPage myPage, Long userId, Integer type) {
        Page page = new Page();
        List<Application> permList = null;
        List<ResourceVo> result = new ArrayList<ResourceVo>();
        return myPage;
    }

    public List<ResourceVo> getAllResource() {
        List<Resource> selectAll = resourceMapper.selectList(null);
        List<ResourceVo> copyTo = ConvertUtil.copyTo(selectAll, ResourceVo.class);
        return copyTo;
    }


    public void addResource(Resource resource) {
        resourceMapper.insert(resource);
    }

    public void delResource(Long resourceId) {
        resourceMapper.deleteById(resourceId);
    }

    public void updateResource(Resource resource) {
        resourceMapper.updateById(resource);
    }


    /**
     * 根据applicationid和applicationEnvId获取当前应用环境的依赖资源
     *
     * @param applicationId
     * @param applicationEnvId
     * @return
     */
    public List<AppResourceRelevance> getRelativeResourceSure(Long applicationId, Long applicationEnvId) {
        LambdaQueryWrapper<AppResourceRelevance> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(AppResourceRelevance::getApplicationId, applicationId)
                .eq(AppResourceRelevance::getApplicationEnvId, applicationEnvId);

        return appResourceRelevanceMapper.selectList(lambdaQueryWrapper);

    }


    /**
     * 获取当前所有资源及应用环境的关联资源
     *
     * @param applicationId
     * @param applicationEnvId
     * @return
     */

    public MyPage<ResourceVo> getAllResourceAndEnv(Long applicationId, Long applicationEnvId) {
//        MyPage<ResourceVo> result = new MyPage<ResourceVo>();
//        Map<Long, Long> relation = this.getRelativeResourceSure(applicationId, applicationEnvId);
//        List<ResourceVo> allResource = this.getAllResource();
//        for (ResourceVo resourceVo : allResource) {
//            List<ResourceEnvVo> envs = resourceEnvService.getAllResourceEnvById(resourceVo.getResourceId());
//            Long resEnvId = relation.get(resourceVo.getResourceId());
//            if (resEnvId != null) {
//                for (ResourceEnvVo resourceEnvVo : envs) {
//                    if (resourceEnvVo.getResourceId().longValue() == resEnvId.longValue()) {
//                        resourceEnvVo.setSelected(true);
//                    }
//                }
//            }
//            resourceVo.setResourceEnvs(envs);
//        }
//        result.setRecords(allResource);
//        result.setCurrent(1);
//        result.setTotal(new Long(allResource.size()));
//        return result;
        return null;
    }
}
